/*-
 * Copyright (c) 2012-2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS: mtblas_1_scp.c    Rev. 0.0.6    2013-10-17T17:11+08:00 $
 */

#include <bscerr.h>

#include <exec_isa.h>
#include <mtblas.h>

int mtblas_swapf(size_t thrz, const size_t sz, float *vx, const step_t incx, float *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_ff, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_swap(size_t thrz, const size_t sz, double *vx, const step_t incx, double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_df, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_swapl(size_t thrz, const size_t sz, long double *vx, const step_t incx, long double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_lf, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_cswapf(size_t thrz, const size_t sz, float complex *vx, const step_t incx, float complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_fc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_cswap(size_t thrz, const size_t sz, double complex *vx, const step_t incx, double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_dc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_cswapl(size_t thrz, const size_t sz, long double complex *vx, const step_t incx, long double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_swap_lc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_copyf(size_t thrz, const size_t sz, const float *vx, const step_t incx, float *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_ff, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_copy(size_t thrz, const size_t sz, const double *vx, const step_t incx, double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_df, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_copyl(size_t thrz, const size_t sz, const long double *vx, const step_t incx, long double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_lf, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_ccopyf(size_t thrz, const size_t sz, const float complex *vx, const step_t incx, float complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_fc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_ccopy(size_t thrz, const size_t sz, const double complex *vx, const step_t incx, double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_dc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_ccopyl(size_t thrz, const size_t sz, const long double complex *vx, const step_t incx, long double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;

    thrz = proper_thrz(thrz);
    ierr = operands_create_2(thrz, &ra, &rb);

    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, ra);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double complex) * incy, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_lc, sz, ra, rb, NULL, NULL);

    ierr = operands_delete_2(ra, rb);

    return ierr;
}


int mtblas_axpyf(size_t thrz, const size_t sz, const float alpha, const float *vx, const step_t incx, float *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_ff, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}


int mtblas_axpy(size_t thrz, const size_t sz, const double alpha, const double *vx, const step_t incx, double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_df, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}


int mtblas_axpyl(size_t thrz, const size_t sz, const long double alpha, const long double *vx, const step_t incx, long double *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_lf, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}


int mtblas_caxpyf(size_t thrz, const size_t sz, const float complex alpha, const float complex *vx, const step_t incx, float complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(float complex) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(float complex) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_fc, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}


int mtblas_caxpy(size_t thrz, const size_t sz, const double complex alpha, const double complex *vx, const step_t incx, double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(double complex) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(double complex) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_dc, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}


int mtblas_caxpyl(size_t thrz, const size_t sz, const long double complex alpha, const long double complex *vx, const step_t incx, long double complex *vy, const step_t incy)
{
    int ierr = ERRNO_UNDEFINED;

    operand_t *ra;
    operand_t *rb;
    operand_t *rc;

    thrz = proper_thrz(thrz);
    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    ierr = operands_batch_set(thrz, (void*) &alpha, 0, ra);
    ierr = operands_distributor(thrz, sz, (void*) vx, sizeof(long double complex) * incx, rb);
    ierr = operands_distributor(thrz, sz, (void*) vy, sizeof(long double complex) * incy, rc);

    ierr = thread_execute(thrz, (void*) op3_fmac_lc, sz, ra, rb, rc, NULL);

    ierr = operands_delete_3(ra, rb, rc);

    return ierr;
}
